package com.roundarch.www.ui;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.roundarch.www.model.Files;
import com.roundarch.www.model.Post;
import com.roundarch.www.model.User;
import com.roundarch.www.service.EntityService;
import com.roundarch.www.util.propertyeditor.EntityPropertyEditor;

@RequestMapping("/files")
@Controller
public class FilesController extends AbstractEntityController<Files> {
	
	
	private final Log logger = LogFactory.getLog(getEntityClass());

	@RequestMapping(value = "/save" , method = RequestMethod.POST)
	public String save(@Valid @ModelAttribute("entity") Files entity,BindingResult result, Model model,@RequestParam("filePath") MultipartFile filePath)
	{
		try{
			 entity.setFilename(filePath.getOriginalFilename());
			 entity.setType(filePath.getContentType());
			 entity.setFile(filePath.getBytes());
			 getEntityService().save(entity);
			 String fileNamePath = filePath.getOriginalFilename();
		     File dest = new File(fileNamePath);
		     dest.createNewFile();
		     filePath.transferTo(dest);
		        

		 } catch (IllegalStateException e) {
	            e.printStackTrace();
	            logger.info("File uploaded failed:");
	     } catch (IOException e) {
	            e.printStackTrace();
	            logger.info("File uploaded failed:");
	     }

		return "redirect:/post/show?id="+entity.getPost().getId();
	}
	
	
	
	
	
	
	@RequestMapping("/download")
	public void download(@RequestParam("id") String id, HttpServletResponse response){
		if(id != null){
			Files file = getEntityService().find(Long.parseLong(id), Files.class);
			response.setContentType(file.getType());
			response.setContentLength(file.getFile().length);
			response.setHeader("Content-Disposition", "attachment;filename=\""+file.getFilename());
			
			try {
				FileCopyUtils.copy(file.getFile(), response.getOutputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	@Override
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		super.initBinder(binder);

		binder.registerCustomEditor(Post.class,	new EntityPropertyEditor<Post>(getEntityService(),Post.class));
		
	}
	
	@Override
	protected String buildDeleteTargetUrl(String deleteTarget) {
		// TODO Auto-generated method stub
		return deleteTarget;
	}
	
	
}
